﻿using RFIDSystem.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDSystem
{
    public partial class Login : BaseForm
    {
        public Login()
        {
            if(DAL.Database.CreateIfNotExists())//无数据库，初始化
            {
                List<role> roles = new List<role>();
                var roleAdmin = new role() { role_id = 1, role_name = "管理员" };
                var roleSale = new role() { role_id = 2, role_name = "销售员" };
                var roleStock = new role() { role_id = 3, role_name = "库管员" };
                roles.Add(roleAdmin);
                roles.Add(roleSale);
                roles.Add(roleStock);

                List<user> users = new List<RFIDSystem.DAL.user>();
                users.Add(new user() { role = roleAdmin, username = "admin", password = "123456" });
                users.Add(new user() { role = roleSale, username = "saler", password = "123456" });
                users.Add(new user() { role = roleStock, username = "stock", password = "123456" });

                DAL.roles.AddRange(roles);
                DAL.users.AddRange(users);
                DAL.SaveChanges();

            }

            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text.Trim() == "")
            {
                MessageBox.Show("请填写用户名");
                return;
            }
            if (txtPassword.Text.Trim()=="")
            {
                MessageBox.Show("请填写密码");
                return;
            }

            var user=DAL.users.Where(u => u.username == txtUsername.Text && u.password == txtPassword.Text).FirstOrDefault();

            if (user != null)
            {

                BaseForm.CruuentRoleId = user.role.role_id;
                BaseForm.CurrentUserId = user.user_id;
                BaseForm.CurrentUserName = user.username;

                this.DialogResult = DialogResult.OK;
            }else
            {
                MessageBox.Show("用户名与密码不符，请重新登录");
            }
        }

        public override bool IsCheckLogin
        {
            get
            {
                return false;
            }
        }
    }
}
